/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package back;

import core.SchoolMat;
import core.Subject;
import dao.ISchoolMat;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import javax.ejb.EJB;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ManagedProperty;
import javax.faces.bean.SessionScoped;
import javax.faces.model.SelectItem;
import java.util.Locale;

/**
 *
 * @author Lukin
 */
@ManagedBean(name = "schoolmat")
@SessionScoped
public class SchoolMatBean implements Serializable {

    @EJB
    ISchoolMat m;
    private SchoolMat mat = null;
    private List<SelectItem> allSchoolMat;
    private List<SchoolMat> selectedSchoolMat;
    private String selectedSubject;

    public String getSelectedSubject() {
        return selectedSubject;
    }

    public void setSelectedSubject(String selectedSubject) {
        this.selectedSubject = selectedSubject;
    }

    // private List<SchoolMat> schoolMatsByYear;
    public SchoolMatBean() {
    }

    public List<SchoolMat> getAllSchoolMat() {
        return m.findAllSchoolMats();
    }

    //TODO tohle by bylo pak lepsi naimplementovat primo v UserDAO
    // protoze dotazy sql se provedou mnohem rychleji nez selektovat materialy
    //az tady
    //@PostConstruct
    public List<SchoolMat> schoolMatsByYear(int year) {
        //System.out.println(year.getYear());
        selectedSchoolMat = new ArrayList<SchoolMat>();
        for (SchoolMat s : m.findAllSchoolMats()) {
            if (s.getYear() == year) {

                boolean isIn = false;
                for (int i = 0; i < selectedSchoolMat.size(); i++) {
                    if (selectedSchoolMat.get(i).getSubject().equals(s.getSubject())) {
                        isIn = true;
                        break;
                    }
                }
                if (!isIn) {
                    selectedSchoolMat.add(s);
                }
            }
        }
        return selectedSchoolMat;

    }

    public List<SchoolMat> schoolMatsBySubject(SchoolMat sub) {

        selectedSchoolMat = new ArrayList<SchoolMat>();

        List<SchoolMat> all = m.findAllSchoolMats();
        
        for (SchoolMat sm : all){
            if (sm.getYear() == sub.getYear()){
                if (sm.getSubject().getName() == null ? sub.getSubject().getName() == null : sm.getSubject().getName().equals(sub.getSubject().getName())){
                    selectedSchoolMat.add(sm);
                }
                        
            }
        }


        return selectedSchoolMat;

    }

    public List<SchoolMat> getSelectedSchoolMat() {
        return selectedSchoolMat;
    }

    public void setSelectedSchoolMat(List<SchoolMat> selectedSchoolMat) {
        this.selectedSchoolMat = selectedSchoolMat;
    }

    public int ratingInt() {
        if (mat != null) {
            return (int) Math.round(mat.getRating());
        } else {
            return 0;
        }

    }

    public void setAllSchoolMat(List<SelectItem> allSchoolMat) {
        this.allSchoolMat = allSchoolMat;
    }

    public List<SelectItem> getAllUsersItemList() {
        allSchoolMat = new ArrayList<SelectItem>();
        for (Iterator<SchoolMat> it = m.findAllSchoolMats().iterator(); it.hasNext();) {
            SchoolMat e = it.next();
            allSchoolMat.add(new SelectItem(e, e.getName() + " " + e.getName()));
        }
        return allSchoolMat;
    }

    public String pickSubject(SchoolMat mat) {
        this.mat = mat;
        return "SchoolMatSubjectDetail?faces-redirect=true";
    }

    public String showDetail(SchoolMat mat) {
        this.mat = mat;
        
        
        
        return "docViewer?faces-redirect=true";
    }

    public SchoolMat getMat() {
        return mat;
    }

    public void setMat(SchoolMat mat) {
        this.mat = mat;
    }

    public List<SchoolMat> pickSearchwordMats(String searchWord) {
        List<SchoolMat> list = m.findAllSchoolMats();
        searchWord = searchWord.toLowerCase(Locale.US);
        int size = list.size();
        for (int i = 0; i < size; i++) {
            if (!list.get(i).getName().toLowerCase(Locale.US).contains(searchWord)
                    && !list.get(i).getSubject().getName().toLowerCase(Locale.US).contains(searchWord)
                    && !list.get(i).getSubject().getDepartment().toLowerCase(Locale.US).contains(searchWord)
                    && !list.get(i).getSubject().getShortcut().toLowerCase(Locale.US).contains(searchWord)) {
                list.remove(i);
                size--;
                i--;
            }
        }
        return list;
    }

    public List<SchoolMat> pickSelectSchoolMats(String subject) {
        List<SchoolMat> list = m.findAllSchoolMats();
        int size = list.size();
        for (int i = 0; i < size; i++) {
            if (!list.get(i).getSubject().getName().equals(subject)) {
                list.remove(i);
                size--;
                i--;
            }
        }
        return list;
    }

    public List<SchoolMat> pickSearchwordSubjMats(String word, String sub) {
        List<SchoolMat> list = m.findAllSchoolMats();
        word = word.toLowerCase(Locale.US);
        String name = sub.toLowerCase(Locale.US);
        int size = list.size();
        for (int i = 0; i < size; i++) {
            if (!list.get(i).getName().toLowerCase(Locale.US).contains(word)
                    || !list.get(i).getSubject().getName().toLowerCase(Locale.US).contains(name)) {
                list.remove(i);
                size--;
                i--;
            }
        }
        return list;
    }
}
